package com.lagou.service;

import com.lagou.annotion.MyAutowired;
import com.lagou.annotion.MyService;
import com.lagou.annotion.MyTransactional;
import com.lagou.dao.AccountDao;
import com.lagou.pojo.Account;

/**
 * @Author ZHAO Yudong
 * @Date 2020/11/6 14:17
 * @description：
 */
@MyService("transferService")
@MyTransactional
public class AccountServiceImpl implements AccountService {

	@MyAutowired
	private AccountDao accountDao;

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}


	@Override
	public int transfer(String fromCardNo, String toCardNo, int money) throws Exception {

		Account from = accountDao.queryAccountByCardNo(fromCardNo);
		Account to = accountDao.queryAccountByCardNo(toCardNo);

		from.setMoney(from.getMoney() - money);
		to.setMoney(to.getMoney() + money);

		int x = accountDao.updateAccountByCardNo(to);
		//测试事务回滚
		int y = accountDao.updateAccountByCardNo(from);

		return x & y;

	}
}

